<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">




<script>
    (function(){
        if(''){
            if (prompt('请输入文章密码') !== ''){
                alert('密码错误！');
                history.back();
            }
        }
    })();
  </script>






<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />



  <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico?v=5.1.4">







  <meta name="keywords" content="iOS," />





  <link rel="alternate" href="/atom.xml" title="Swift社区" type="application/atom+xml" />






<meta name="description" content="一：介绍React Native (简称RN)是Facebook于2015年4月开源的跨平台移动应用开发框架，是Facebook早先开源的JS框架 React 在原生移动应用平台的衍生产物，目前支持iOS和安卓两大平台。RN使用Javascript语言，类似于HTML的JSX，以及CSS来开发移动应用，因此熟悉Web前端开发的技术人员只需很少的学习就可以进入移动应用开发领域。">
<meta name="keywords" content="iOS">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS--React Native网络请求插件">
<meta property="og:url" content="https://fanbaoying.github.io/iOS-React-Native网络请求插件/index.html">
<meta property="og:site_name" content="Swift社区">
<meta property="og:description" content="一：介绍React Native (简称RN)是Facebook于2015年4月开源的跨平台移动应用开发框架，是Facebook早先开源的JS框架 React 在原生移动应用平台的衍生产物，目前支持iOS和安卓两大平台。RN使用Javascript语言，类似于HTML的JSX，以及CSS来开发移动应用，因此熟悉Web前端开发的技术人员只需很少的学习就可以进入移动应用开发领域。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2829694-48307b4d71bc5800.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/300">
<meta property="og:updated_time" content="2018-12-16T10:02:17.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="iOS--React Native网络请求插件">
<meta name="twitter:description" content="一：介绍React Native (简称RN)是Facebook于2015年4月开源的跨平台移动应用开发框架，是Facebook早先开源的JS框架 React 在原生移动应用平台的衍生产物，目前支持iOS和安卓两大平台。RN使用Javascript语言，类似于HTML的JSX，以及CSS来开发移动应用，因此熟悉Web前端开发的技术人员只需很少的学习就可以进入移动应用开发领域。">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/2829694-48307b4d71bc5800.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/300">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://fanbaoying.github.io/iOS-React-Native网络请求插件/"/>





  <title>iOS--React Native网络请求插件 | Swift社区</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
    
    <a href="https://github.com/SwiftCommunityRes" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    
    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Swift社区</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">做最好的 Swift 社区</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://fanbaoying.github.io/iOS-React-Native网络请求插件/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Swift社区">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://avatars.githubusercontent.com/u/84354365?v=4">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Swift社区">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">iOS--React Native网络请求插件</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-12-16T17:11:01+08:00">
                2018-12-16
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/iOS-React-Native集/" itemprop="url" rel="index">
                    <span itemprop="name">iOS-React Native集</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>

                <!-- 
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                -->
                <span title="字数统计">
                  数字统计 2,690字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>

                <!-- 
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                --> 

                <span title="阅读时长">
                  阅读时长 13分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="一：介绍"><a href="#一：介绍" class="headerlink" title="一：介绍"></a>一：介绍</h2><p>React Native (简称RN)是Facebook于2015年4月开源的跨平台移动应用开发框架，是Facebook早先开源的JS框架 React 在原生移动应用平台的衍生产物，目前支持iOS和安卓两大平台。RN使用Javascript语言，类似于HTML的JSX，以及CSS来开发移动应用，因此熟悉Web前端开发的技术人员只需很少的学习就可以进入移动应用开发领域。</p>
<a id="more"></a>
<p>在React Native移动平台项目开发中，除了React Native 提供的封装好的部分插件和原声组建外，在实际的项目中还需要使用到很多其他的插件，比如网络请求、数据库、相机、相册、通讯录、视频播放器、浏览器、蓝牙连接、图片处理、消息推送、地图、统计、埋点等等APP开发中需要用到的功能，都为IDE开发平台提供封装好的插件，以便项目开发使用。</p>
<p>另外，这些博文都是来源于我日常开发中的技术总结，在时间允许的情况下，我会针对技术点分别分享iOS、Android两个版本，如果有其他技术点需要，可在文章后留言，我会尽全力帮助大家。这篇文章重点介绍网络请求插件的开发与使用</p>
<h2 id="二：实现思路分析"><a href="#二：实现思路分析" class="headerlink" title="二：实现思路分析"></a>二：实现思路分析</h2><p>网络请求插件是需要实现前端与服务端的数据交互，其中包括GET请求、POST请求、文件上传、单/多张图片上传、文件下载等功能。这些功能将通过封装后的方法暴漏出来，通过RN接口提供给Javascript开发使用。</p>
<p>具体的实现思路如下：</p>
<ol>
<li><p>新建NetWorkPlugin类，实现RCTBridgeModule协议</p>
</li>
<li><p>添加RCT_EXPORT_MODULE()宏</p>
</li>
<li><p>添加React Native跟控制器</p>
</li>
<li><p>声明被JavaScript 调用的方法</p>
</li>
<li><p>导入AFNetworking请求库</p>
</li>
<li><p>新建NetworkHelper类，封装实现网络请求功能</p>
</li>
<li><p>实现GET请求</p>
</li>
<li><p>实现POST请求</p>
</li>
<li><p>实现文件上传</p>
</li>
<li><p>实现单/多张图片上传</p>
</li>
<li><p>实现文件下载</p>
</li>
<li><p>Javascript调用浏览器方法</p>
</li>
</ol>
<h2 id="三：实现源码分析"><a href="#三：实现源码分析" class="headerlink" title="三：实现源码分析"></a>三：实现源码分析</h2><h5 id="1-新建NetWorkPlugin类，实现RCTBridgeModule协议"><a href="#1-新建NetWorkPlugin类，实现RCTBridgeModule协议" class="headerlink" title="1. 新建NetWorkPlugin类，实现RCTBridgeModule协议"></a>1. 新建NetWorkPlugin类，实现RCTBridgeModule协议</h5><p>新建继承NSObject的NetWorkPlugin类，并实现RCTBridgeModule协议<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// NetWorkPlugin.h</span><br><span class="line">#import &lt;Foundation/Foundation.h&gt;</span><br><span class="line">#import &lt;React/RCTBridgeModule.h&gt;</span><br><span class="line">#import &lt;UIKit/UIKit.h&gt;</span><br><span class="line">@interface NetWorkPlugin : NSObject&lt;RCTBridgeModule&gt;</span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<h5 id="2-添加RCT-EXPORT-MODULE-宏"><a href="#2-添加RCT-EXPORT-MODULE-宏" class="headerlink" title="2. 添加RCT_EXPORT_MODULE()宏"></a>2. 添加RCT_EXPORT_MODULE()宏</h5><p>为了实现RCTBridgeModule协议，NetWorkPlugin的类需要包含RCT_EXPORT_MODULE()宏。<br>并在这个宏里面添加一个参数“NetWorkPlugin”用来指定在 JavaScript 中访问这个模块的名字。<br>如果你不指定，默认就会使用这个 Objective-C 类的名字。<br>如果类名以 RCT 开头，则 JavaScript 端引入的模块名会自动移除这个前缀。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">// NetWorkPlugin.m</span><br><span class="line">#import &quot;NetWorkPlugin.h&quot;</span><br><span class="line">@implementation NetWorkPlugin</span><br><span class="line">RCT_EXPORT_MODULE(NetWorkPlugin);</span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<h5 id="3-添加React-Native跟控制器"><a href="#3-添加React-Native跟控制器" class="headerlink" title="3. 添加React Native跟控制器"></a>3. 添加React Native跟控制器</h5><p>如果不添加React Native跟控制器，view将不能正常显示出来，实现方法如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">// NetWorkPlugin.m</span><br><span class="line">#import &lt;React/RCTUtils.h&gt;</span><br></pre></td></tr></table></figure></p>
<p>引入<react rctutils.h="">之后，在视图初始化或者显示的时候，按照如下方法调用即可<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">UIViewController *vc = RCTPresentedViewController();</span><br></pre></td></tr></table></figure></react></p>
<h5 id="4-声明被JavaScript-调用的方法"><a href="#4-声明被JavaScript-调用的方法" class="headerlink" title="4. 声明被JavaScript 调用的方法"></a>4. 声明被JavaScript 调用的方法</h5><p>React Native需要明确的声明要给 JavaScript 导出的方法，否则 React Native 不会导出任何方法。下面通过举例来展示声明的方法，通过RCT_EXPORT_METHOD()宏来实现：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">// NetWorkPlugin.m</span><br><span class="line">#import &quot;NetWorkPlugin.h&quot;</span><br><span class="line">#import &lt;React/RCTUtils.h&gt;</span><br><span class="line">@implementation NetWorkPlugin</span><br><span class="line">RCT_EXPORT_MODULE(NetWorkPlugin);</span><br><span class="line">RCT_EXPORT_METHOD(post:(NSDictionary *)arguments</span><br><span class="line">                  withCompletionHandler:(RCTResponseSenderBlock)completion</span><br><span class="line">                  failureHandler:(RCTResponseSenderBlock)failure)</span><br><span class="line">&#123;</span><br><span class="line">    NSLog(@&quot;POST网络请求执行方法&quot;);</span><br><span class="line">&#125;</span><br><span class="line">@end</span><br></pre></td></tr></table></figure>
<h5 id="5-导入AFNetworking请求库"><a href="#5-导入AFNetworking请求库" class="headerlink" title="5. 导入AFNetworking请求库"></a>5. 导入AFNetworking请求库</h5><p>网络请求使用的第三方库是AFNetworking，这个库很常见，也比较常用，就不做过多的描述，可手动导入也可使用cocoapods自动导入，导入之后在.m文件中引入头文件。</p>
<h5 id="6-新建NetworkHelper类，封装实现网络请求功能"><a href="#6-新建NetworkHelper类，封装实现网络请求功能" class="headerlink" title="6. 新建NetworkHelper类，封装实现网络请求功能"></a>6. 新建NetworkHelper类，封装实现网络请求功能</h5><p>新建继承NSObject的NetworkHelper类，定义枚举类型来判断网络状态：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">typedef NS_ENUM(NSUInteger, NetworkStatusType) &#123;</span><br><span class="line">    /** 未知网络*/</span><br><span class="line">    NetworkStatusUnknown,</span><br><span class="line">    /** 无网络*/</span><br><span class="line">    NetworkStatusNotReachable,</span><br><span class="line">    /** 手机网络*/</span><br><span class="line">    NetworkStatusReachableViaWWAN,</span><br><span class="line">    /** WIFI网络*/</span><br><span class="line">    NetworkStatusReachableViaWiFi</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<p>定义网络状态的Block<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">typedef void(^NetworkStatus)(NetworkStatusType status);</span><br></pre></td></tr></table></figure></p>
<p>实时获取网络状态,通过Block回调实时获取(此方法可多次调用)<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">+ (void)networkStatusWithBlock:(NetworkStatus)networkStatus;</span><br></pre></td></tr></table></figure></p>
<h5 id="7-实现GET请求"><a href="#7-实现GET请求" class="headerlink" title="7. 实现GET请求"></a>7. 实现GET请求</h5><p>声明GET请求方法:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> *</span><br><span class="line"> *  @param URL        请求地址</span><br><span class="line"> *  @param parameters 请求参数</span><br><span class="line"> *  @param success    请求成功的回调</span><br><span class="line"> *  @param failure    请求失败的回调</span><br><span class="line"> *</span><br><span class="line"> *  @return 返回的对象可取消请求,调用cancel方法</span><br><span class="line"> */</span><br><span class="line">+ (__kindof NSURLSessionTask *)GET:(NSString *)URL</span><br><span class="line">                        parameters:(id)parameters</span><br><span class="line">                           success:(HttpRequestSuccess)success</span><br><span class="line">                           failure:(HttpRequestFailed)failure;</span><br></pre></td></tr></table></figure></p>
<h5 id="8-实现POST请求"><a href="#8-实现POST请求" class="headerlink" title="8. 实现POST请求"></a>8. 实现POST请求</h5><p>声明POST请求方法:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> *</span><br><span class="line"> *  @param URL        请求地址</span><br><span class="line"> *  @param parameters 请求参数</span><br><span class="line"> *  @param success    请求成功的回调</span><br><span class="line"> *  @param failure    请求失败的回调</span><br><span class="line"> *</span><br><span class="line"> *  @return 返回的对象可取消请求,调用cancel方法</span><br><span class="line"> */</span><br><span class="line">+ (__kindof NSURLSessionTask *)POST:(NSString *)URL</span><br><span class="line">                         parameters:(id)parameters</span><br><span class="line">                            success:(HttpRequestSuccess)success</span><br><span class="line">                            failure:(HttpRequestFailed)failure;</span><br></pre></td></tr></table></figure></p>
<p>POST请求具体的方法实现如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">+ (NSURLSessionTask *)POST:(NSString *)URL</span><br><span class="line">                parameters:(id)parameters</span><br><span class="line">                   success:(HttpRequestSuccess)success</span><br><span class="line">                   failure:(HttpRequestFailed)failure &#123;</span><br><span class="line">    NSString *AllReplaceURL = [self replaceURL:URL];</span><br><span class="line">    [self setAFHTTPSessionManagerProperty:^(AFHTTPSessionManager *sessionManager) &#123;</span><br><span class="line">        [sessionManager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) &#123;</span><br><span class="line">            return parameters;</span><br><span class="line">        &#125;];</span><br><span class="line">    &#125;];</span><br><span class="line">    NSURLSessionTask *sessionTask = [_sessionManager POST:AllReplaceURL parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) &#123;</span><br><span class="line"></span><br><span class="line">    &#125; success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) &#123;</span><br><span class="line"></span><br><span class="line">        if (_isOpenLog) &#123;NSLog(@&quot;responseObject = %@&quot;,[self jsonToString:responseObject]);&#125;</span><br><span class="line">        [[self allSessionTask] removeObject:task];</span><br><span class="line">        success ? success(responseObject) : nil;</span><br><span class="line"></span><br><span class="line">    &#125; failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) &#123;</span><br><span class="line"></span><br><span class="line">        if (_isOpenLog) &#123;NSLog(@&quot;error = %@&quot;,error);&#125;</span><br><span class="line">        [[self allSessionTask] removeObject:task];</span><br><span class="line">        failure ? failure(error) : nil;</span><br><span class="line"></span><br><span class="line">    &#125;];</span><br><span class="line">    sessionTask ? [[self allSessionTask] addObject:sessionTask] : nil ;</span><br><span class="line">    return sessionTask;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h5 id="9-实现文件上传"><a href="#9-实现文件上传" class="headerlink" title="9. 实现文件上传"></a>9. 实现文件上传</h5><p>声明文件上传方法:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> *</span><br><span class="line"> *  @param URL        请求地址</span><br><span class="line"> *  @param parameters 请求参数</span><br><span class="line"> *  @param names       文件对应服务器上的字段</span><br><span class="line"> *  @param filePaths   文件本地的沙盒路径</span><br><span class="line"> *  @param progress   上传进度信息</span><br><span class="line"> *  @param success    请求成功的回调</span><br><span class="line"> *  @param failure    请求失败的回调</span><br><span class="line"> *</span><br><span class="line"> *  @return 返回的对象可取消请求,调用cancel方法</span><br><span class="line"> */</span><br><span class="line">+ (__kindof NSURLSessionTask *)uploadFileWithURL:(NSString *)URL</span><br><span class="line">                                      parameters:(id)parameters</span><br><span class="line">                                            names:(NSArray&lt;NSString *&gt; *)names</span><br><span class="line">                                        filePaths:(NSArray&lt;NSString *&gt; *)filePaths</span><br><span class="line">                                        progress:(HttpProgress)progress</span><br><span class="line">                                         success:(HttpRequestSuccess)success</span><br><span class="line">                                         failure:(HttpRequestFailed)failure;</span><br></pre></td></tr></table></figure></p>
<p>文件上传具体的方法实现如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">+ (NSURLSessionTask *)uploadFileWithURL:(NSString *)URL</span><br><span class="line">                             parameters:(id)parameters</span><br><span class="line">                                  names:(NSArray&lt;NSString *&gt; *)names</span><br><span class="line">                              filePaths:(NSArray&lt;NSString *&gt; *)filePaths</span><br><span class="line">                               progress:(HttpProgress)progress</span><br><span class="line">                                success:(HttpRequestSuccess)success</span><br><span class="line">                                failure:(HttpRequestFailed)failure &#123;</span><br><span class="line"></span><br><span class="line">    NSString *AllReplaceURL = [self replaceURL:URL];</span><br><span class="line"></span><br><span class="line">    NSURLSessionTask *sessionTask = [_sessionManager POST:AllReplaceURL parameters:parameters constructingBodyWithBlock:^(id&lt;AFMultipartFormData&gt;  _Nonnull formData) &#123;</span><br><span class="line">        NSError *error = nil;</span><br><span class="line">        for (NSUInteger i = 0; i &lt; filePaths.count; i++) &#123;</span><br><span class="line">            NSString *name = names[i];</span><br><span class="line">            NSString *filePath = filePaths[i];</span><br><span class="line">            [formData appendPartWithFileURL:[NSURL fileURLWithPath:filePath] name:name error:&amp;error];</span><br><span class="line">        &#125;</span><br><span class="line">        (failure &amp;&amp; error) ? failure(error) : nil;</span><br><span class="line">    &#125; progress:^(NSProgress * _Nonnull uploadProgress) &#123;</span><br><span class="line">        //上传进度</span><br><span class="line">        dispatch_sync(dispatch_get_main_queue(), ^&#123;</span><br><span class="line">            progress ? progress(uploadProgress) : nil;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125; success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) &#123;</span><br><span class="line"></span><br><span class="line">        if (_isOpenLog) &#123;NSLog(@&quot;responseObject = %@&quot;,[self jsonToString:responseObject]);&#125;</span><br><span class="line">        [[self allSessionTask] removeObject:task];</span><br><span class="line">        success ? success(responseObject) : nil;</span><br><span class="line"></span><br><span class="line">    &#125; failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) &#123;</span><br><span class="line"></span><br><span class="line">        if (_isOpenLog) &#123;NSLog(@&quot;error = %@&quot;,error);&#125;</span><br><span class="line">        [[self allSessionTask] removeObject:task];</span><br><span class="line">        failure ? failure(error) : nil;</span><br><span class="line">    &#125;];</span><br><span class="line">    sessionTask ? [[self allSessionTask] addObject:sessionTask] : nil ;</span><br><span class="line">    return sessionTask;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h5 id="10-实现单-多张图片上传"><a href="#10-实现单-多张图片上传" class="headerlink" title="10. 实现单/多张图片上传"></a>10. 实现单/多张图片上传</h5><p>声明单/多张图片上传方法:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> *</span><br><span class="line"> *  @param URL        请求地址</span><br><span class="line"> *  @param parameters 请求参数</span><br><span class="line"> *  @param name       图片对应服务器上的字段</span><br><span class="line"> *  @param images     图片数组</span><br><span class="line"> *  @param fileNames  图片文件名数组, 可以为nil, 数组内的文件名默认为当前日期时间&quot;yyyyMMddHHmmss&quot;</span><br><span class="line"> *  @param imageScale 图片文件压缩比 范围 (0.f ~ 1.f)</span><br><span class="line"> *  @param imageType  图片文件的类型,例:png、jpg(默认类型)....</span><br><span class="line"> *  @param progress   上传进度信息</span><br><span class="line"> *  @param success    请求成功的回调</span><br><span class="line"> *  @param failure    请求失败的回调</span><br><span class="line"> *</span><br><span class="line"> *  @return 返回的对象可取消请求,调用cancel方法</span><br><span class="line"> */</span><br><span class="line">+ (__kindof NSURLSessionTask *)uploadImagesWithURL:(NSString *)URL</span><br><span class="line">                                        parameters:(id)parameters</span><br><span class="line">                                              name:(NSString *)name</span><br><span class="line">                                            images:(NSArray&lt;UIImage *&gt; *)images</span><br><span class="line">                                         fileNames:(NSArray&lt;NSString *&gt; *)fileNames</span><br><span class="line">                                        imageScale:(CGFloat)imageScale</span><br><span class="line">                                         imageType:(NSString *)imageType</span><br><span class="line">                                          progress:(HttpProgress)progress</span><br><span class="line">                                           success:(HttpRequestSuccess)success</span><br><span class="line">                                           failure:(HttpRequestFailed)failure;</span><br></pre></td></tr></table></figure></p>
<p>图片经过等比压缩后得到的二进制文件，默认图片的文件名, 若fileNames为nil就使用，单/多张图片上传具体的方法实现如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">+ (NSURLSessionTask *)uploadImagesWithURL:(NSString *)URL</span><br><span class="line">                               parameters:(id)parameters</span><br><span class="line">                                     name:(NSString *)name</span><br><span class="line">                                   images:(NSArray&lt;UIImage *&gt; *)images</span><br><span class="line">                                fileNames:(NSArray&lt;NSString *&gt; *)fileNames</span><br><span class="line">                               imageScale:(CGFloat)imageScale</span><br><span class="line">                                imageType:(NSString *)imageType</span><br><span class="line">                                 progress:(HttpProgress)progress</span><br><span class="line">                                  success:(HttpRequestSuccess)success</span><br><span class="line">                                  failure:(HttpRequestFailed)failure &#123;</span><br><span class="line"></span><br><span class="line">    NSString *AllReplaceURL = [self replaceURL:URL];</span><br><span class="line"></span><br><span class="line">    NSURLSessionTask *sessionTask = [_sessionManager POST:AllReplaceURL parameters:parameters constructingBodyWithBlock:^(id&lt;AFMultipartFormData&gt;  _Nonnull formData) &#123;</span><br><span class="line"></span><br><span class="line">        for (NSUInteger i = 0; i &lt; images.count; i++) &#123;</span><br><span class="line">            NSData *imageData = UIImageJPEGRepresentation(images[i], imageScale ?: 1.f);</span><br><span class="line"></span><br><span class="line">            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];</span><br><span class="line">            formatter.dateFormat = @&quot;yyyyMMddHHmmss&quot;;</span><br><span class="line">            NSString *str = [formatter stringFromDate:[NSDate date]];</span><br><span class="line">            NSString *imageFileName = NSStringFormat(@&quot;%@%ld.%@&quot;,str,i,imageType?:@&quot;jpg&quot;);</span><br><span class="line"></span><br><span class="line">            [formData appendPartWithFileData:imageData</span><br><span class="line">                                        name:name</span><br><span class="line">                                    fileName:fileNames ? NSStringFormat(@&quot;%@.%@&quot;,fileNames[i],imageType?:@&quot;jpg&quot;) : imageFileName</span><br><span class="line">                                    mimeType:NSStringFormat(@&quot;image/%@&quot;,imageType ?: @&quot;jpg&quot;)];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125; progress:^(NSProgress * _Nonnull uploadProgress) &#123;</span><br><span class="line">        dispatch_sync(dispatch_get_main_queue(), ^&#123;</span><br><span class="line">            progress ? progress(uploadProgress) : nil;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125; success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) &#123;</span><br><span class="line"></span><br><span class="line">        if (_isOpenLog) &#123;NSLog(@&quot;responseObject = %@&quot;,[self jsonToString:responseObject]);&#125;</span><br><span class="line">        [[self allSessionTask] removeObject:task];</span><br><span class="line">        success ? success(responseObject) : nil;</span><br><span class="line"></span><br><span class="line">    &#125; failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) &#123;</span><br><span class="line"></span><br><span class="line">        if (_isOpenLog) &#123;NSLog(@&quot;error = %@&quot;,error);&#125;</span><br><span class="line">        [[self allSessionTask] removeObject:task];</span><br><span class="line">        failure ? failure(error) : nil;</span><br><span class="line">    &#125;];</span><br><span class="line">    sessionTask ? [[self allSessionTask] addObject:sessionTask] : nil ;</span><br><span class="line">    return sessionTask;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h5 id="11-实现文件下载"><a href="#11-实现文件下载" class="headerlink" title="11. 实现文件下载"></a>11. 实现文件下载</h5><p>声明文件下载方法:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> *</span><br><span class="line"> *  @param URL      请求地址</span><br><span class="line"> *  @param fileDir  文件存储目录(默认存储目录为Download)</span><br><span class="line"> *  @param progress 文件下载的进度信息</span><br><span class="line"> *  @param success  下载成功的回调(回调参数filePath:文件的路径)</span><br><span class="line"> *  @param failure  下载失败的回调</span><br><span class="line"> *</span><br><span class="line"> *  @return 返回NSURLSessionDownloadTask实例，可用于暂停继续，暂停调用suspend方法，开始下载调用resume方法</span><br><span class="line"> */</span><br><span class="line">+ (__kindof NSURLSessionTask *)downloadWithURL:(NSString *)URL</span><br><span class="line">                                       fileDir:(NSString *)fileDir</span><br><span class="line">                                      progress:(HttpProgress)progress</span><br><span class="line">                                       success:(void(^)(NSString *filePath))success</span><br><span class="line">                                       failure:(HttpRequestFailed)failure;</span><br></pre></td></tr></table></figure></p>
<p>在下载过程中可以获取到下载进度，下载流程为：缓存目录拼接完成，打开文件管理器，创建Download目录，拼接文件路径，返回文件位置的URL路径。文件下载具体的方法实现如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">+ (NSURLSessionTask *)downloadWithURL:(NSString *)URL</span><br><span class="line">                              fileDir:(NSString *)fileDir</span><br><span class="line">                             progress:(HttpProgress)progress</span><br><span class="line">                              success:(void(^)(NSString *))success</span><br><span class="line">                              failure:(HttpRequestFailed)failure &#123;</span><br><span class="line">    NSString *AllReplaceURL = [self replaceURL:URL];</span><br><span class="line">    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:AllReplaceURL]];</span><br><span class="line">    __block NSURLSessionDownloadTask *downloadTask = [_sessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) &#123;</span><br><span class="line">        dispatch_sync(dispatch_get_main_queue(), ^&#123;</span><br><span class="line">            progress ? progress(downloadProgress) : nil;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125; destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) &#123;</span><br><span class="line">        NSString *downloadDir = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:fileDir ? fileDir : @&quot;Download&quot;];</span><br><span class="line"></span><br><span class="line">        NSFileManager *fileManager = [NSFileManager defaultManager];</span><br><span class="line"></span><br><span class="line">        [fileManager createDirectoryAtPath:downloadDir withIntermediateDirectories:YES attributes:nil error:nil];</span><br><span class="line"></span><br><span class="line">        NSString *filePath = [downloadDir stringByAppendingPathComponent:response.suggestedFilename];</span><br><span class="line"></span><br><span class="line">        return [NSURL fileURLWithPath:filePath];</span><br><span class="line"></span><br><span class="line">    &#125; completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) &#123;</span><br><span class="line"></span><br><span class="line">        [[self allSessionTask] removeObject:downloadTask];</span><br><span class="line">        if(failure &amp;&amp; error) &#123;failure(error) ; return ;&#125;;</span><br><span class="line">        success ? success(filePath.absoluteString /** NSURL-&gt;NSString*/) : nil;</span><br><span class="line"></span><br><span class="line">    &#125;];</span><br><span class="line">    [downloadTask resume];</span><br><span class="line">    downloadTask ? [[self allSessionTask] addObject:downloadTask] : nil ;</span><br><span class="line"></span><br><span class="line">    return downloadTask;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h5 id="12-Javascript调用浏览器方法"><a href="#12-Javascript调用浏览器方法" class="headerlink" title="12. Javascript调用浏览器方法"></a>12. Javascript调用浏览器方法</h5><p>现在从 Javascript 里可以这样调用这个方法：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">import &#123; NativeModules &#125; from &quot;react-native&quot;;</span><br><span class="line">const NetWorkPlugin = NativeModules.NetWorkPlugin;</span><br><span class="line">NetworkPlugin.post(&#123;url:&quot;http://192.168.1.1:8080/ApiSystem/login&quot;,params:&#123;name:&quot;15842137500&quot;,login_type:&quot;0&quot;,password:&quot;000000&quot;&#125;,headers:&#123;&#125;&#125;,(msg) =&gt; &#123;</span><br><span class="line">                                         Alert.alert(JSON.stringify(msg));</span><br><span class="line"></span><br><span class="line">                                         &#125;,(err) =&gt; &#123;</span><br><span class="line">                                         Alert.alert(JSON.stringify(err));</span><br><span class="line">                                         &#125;);</span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>希望可以帮助大家，如有问题可加QQ技术交流群: 668562416</p>
<p>如果哪里有什么不对或者不足的地方，还望读者多多提意见或建议</p>
<p>如需转载请联系我，经过授权方可转载，谢谢</p>
</blockquote>
<h2 id="公众号：网罗开发"><a href="#公众号：网罗开发" class="headerlink" title="公众号：网罗开发"></a>公众号：网罗开发</h2><p><img src="http://upload-images.jianshu.io/upload_images/2829694-48307b4d71bc5800.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/300" alt=""></p>

      
    </div>
    
    
    
    

    
      <div>
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      </div>
    


    
      <div>
        
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <p><span>本文标题:</span><a href="/iOS-React-Native网络请求插件/">iOS--React Native网络请求插件</a></p>
  <p><span>文章作者:</span><a href="/" title="访问 Swift社区 的个人博客">Swift社区</a></p>
  <p><span>发布时间:</span>2018年12月16日 - 17:12</p>
  <p><span>最后更新:</span>2018年12月16日 - 18:12</p>
  <p><span>原始链接:</span><a href="/iOS-React-Native网络请求插件/" title="iOS--React Native网络请求插件">https://fanbaoying.github.io/iOS-React-Native网络请求插件/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://fanbaoying.github.io/iOS-React-Native网络请求插件/"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
      $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
        });
    });  
</script>

      </div>
    


    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat.png" alt="Swift社区 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.png" alt="Swift社区 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/iOS/" rel="tag"><i class="fa fa-tag"></i> iOS</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/iOS-React-Native-原生密码键盘插件/" rel="next" title="iOS-React Native 原生密码键盘插件">
                <i class="fa fa-chevron-left"></i> iOS-React Native 原生密码键盘插件
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/iOS-React-Native浏览器插件/" rel="prev" title="iOS--React Native浏览器插件">
                iOS--React Native浏览器插件 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
<span class="jiathis_txt">分享到：</span>
<a class="jiathis_button_fav">收藏夹</a>
<a class="jiathis_button_copy">复制网址</a>
<a class="jiathis_button_email">邮件</a>
<a class="jiathis_button_weixin">微信</a>
<a class="jiathis_button_qzone">QQ空间</a>
<a class="jiathis_button_tqq">腾讯微博</a>
<a class="jiathis_button_douban">豆瓣</a>
<a class="jiathis_button_share">一键分享</a>

<a href="http://www.jiathis.com/share?uid=2140465" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank">更多</a>
<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
var jiathis_config={
  data_track_clickback:true,
  summary:"",
  shortUrl:false,
  hideMore:false
}
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=" charset="utf-8"></script>
<!-- JiaThis Button END -->
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://avatars.githubusercontent.com/u/84354365?v=4"
                alt="Swift社区" />
            
              <p class="site-author-name" itemprop="name">Swift社区</p>
              <p class="site-description motion-element" itemprop="description">我们的使命是做一个最专业最权威的 Swift 中文社区，我们的愿景是希望更多的人学习和使用Swift。我们会分享以 Swift 实战、SwiftUI、Swift 基础为核心的技术干货，不忘初心，牢记使命。</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">187</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/qq_36478920" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>CSDN</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/5a30d987f265da430d580126" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-spinner"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/fanbaoying" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/swift-community" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>知乎</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/u/7711465033" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-weibo"></i>微博</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzAxNzgzNTgwMw==&action=getalbum&album_id=2024314764775981057" target="_blank" title="公众号">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>公众号</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/SwiftWeekly" title="SwiftWeekly" target="_blank">SwiftWeekly</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/article-ios" title="Article-ios" target="_blank">Article-ios</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/SwiftUI-Book" title="SwiftUI-Book" target="_blank">SwiftUI-Book</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#一：介绍"><span class="nav-number">1.</span> <span class="nav-text">一：介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二：实现思路分析"><span class="nav-number">2.</span> <span class="nav-text">二：实现思路分析</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#三：实现源码分析"><span class="nav-number">3.</span> <span class="nav-text">三：实现源码分析</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-新建NetWorkPlugin类，实现RCTBridgeModule协议"><span class="nav-number">3.0.0.1.</span> <span class="nav-text">1. 新建NetWorkPlugin类，实现RCTBridgeModule协议</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-添加RCT-EXPORT-MODULE-宏"><span class="nav-number">3.0.0.2.</span> <span class="nav-text">2. 添加RCT_EXPORT_MODULE()宏</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-添加React-Native跟控制器"><span class="nav-number">3.0.0.3.</span> <span class="nav-text">3. 添加React Native跟控制器</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#4-声明被JavaScript-调用的方法"><span class="nav-number">3.0.0.4.</span> <span class="nav-text">4. 声明被JavaScript 调用的方法</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-导入AFNetworking请求库"><span class="nav-number">3.0.0.5.</span> <span class="nav-text">5. 导入AFNetworking请求库</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#6-新建NetworkHelper类，封装实现网络请求功能"><span class="nav-number">3.0.0.6.</span> <span class="nav-text">6. 新建NetworkHelper类，封装实现网络请求功能</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#7-实现GET请求"><span class="nav-number">3.0.0.7.</span> <span class="nav-text">7. 实现GET请求</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#8-实现POST请求"><span class="nav-number">3.0.0.8.</span> <span class="nav-text">8. 实现POST请求</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#9-实现文件上传"><span class="nav-number">3.0.0.9.</span> <span class="nav-text">9. 实现文件上传</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#10-实现单-多张图片上传"><span class="nav-number">3.0.0.10.</span> <span class="nav-text">10. 实现单/多张图片上传</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#11-实现文件下载"><span class="nav-number">3.0.0.11.</span> <span class="nav-text">11. 实现文件下载</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#12-Javascript调用浏览器方法"><span class="nav-number">3.0.0.12.</span> <span class="nav-text">12. Javascript调用浏览器方法</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#公众号：网罗开发"><span class="nav-number">4.</span> <span class="nav-text">公众号：网罗开发</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Swift社区</span>

  
</div>

<!-- 
<div class="powered-by">
  <i class="fa fa-user-md">
  </i>
  <span id="busuanzi_container_site_uv">
    已有<span id="busuanzi_value_site_uv"></span>人来访
  </span>

</div>


  <span class="post-meta-divider">|</span>


<div class="powered-by">
  <i class="fa fa-eye"></i>
  <span id="busuanzi_container_site_pv">
      访问量<span id="busuanzi_value_site_pv"></span>次
  </span>
</div>



  <span class="post-meta-divider">|</span>


-->

<div class="theme-info">
  <i class="fa fa-pencil"></i>
  <span class="post-count">博客全站共361.6k字</span>
</div>


<!-- 
  <span class="post-meta-divider">|</span>



  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>



-->

        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
